<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品查询页面</title>
    <base href="<%=request.getContextPath()%>/"/>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <script src="static/js/jquery-2.1.1.min.js"></script>
    <%@include file="layUI.jsp"%>

    <style type="text/css">
        body{
            font-size: 20px;
            color: coral;
            text-align: center;
        }
        a{
            font-size: 25px;
            color: coral;
            text-align: center;
        }
        img{
            width: 150px;
            height: 150px;
        }
        form select{
            margin:10px;
        }
        table{
            width: 730px;border-collapse:separate; border-spacing:0px 15px;
        }
        td{
            border-bottom:2px dashed #00FFFF;
        }
        th{
            border-bottom:2px dashed #00FFFF;
        }
    </style>
</head>
<body>
<form method="post">
    <label>商品名称</label>
    <input type="text" name="goodsName" id="goodsName">
    <label>商家名称</label>
    <select name="sellerName" id="sellerName">
        <option value="-1">全部</option>
    </select>
    <label>小分类名称</label>
    <select name="smallName" id="smallName">
        <option value="-1">全部</option>
    </select>
    <input class="layui-btn" type="button" value="查询" onclick="Fuzzyquery()">
</form>
<table id="newsList" cellpadding="0" cellspacing="0" class="admin-list">
    <tr>
        <th>商品id</th>
        <th>商品名称</th>
        <th>小分类名称</th>
        <th>商品的价格</th>
        <th>商品的数量</th>
        <th>商品的图像</th>
        <th>商品的运费</th>
        <th>商品的类型</th>
        <th>商家名称</th>
        <th>商品的折扣</th>
        <th colspan="2">操作</th>
    </tr>
    <tbody id="tbody1"></tbody>
</table>
<br>
第<b id="pageNo"></b>页&nbsp;&nbsp;
<a class="layui-btn layui-btn-sm" href="javascript:void(0)" onclick="homepage()">首页</a>
<a class="layui-btn layui-btn-sm" href="javascript:void(0)" onclick="Previouspage()">上一页</a>
<a class="layui-btn layui-btn-sm" href="javascript:void(0)" onclick="nextpage()">下一页</a>
<a class="layui-btn layui-btn-sm" href="javascript:void(0)" onclick="Tailpage()">末页</a>
&nbsp;&nbsp;共<b id="totalPages"></b>页&nbsp;&nbsp;
</body>

<script type="text/javascript">

    $(function () {
        CountSelect();
        addSmall();
        addSeller();
    })
    //全查
    function CountSelect() {
        $("#tbody1").html("");
        var goodsName=$("#goodsName").val();
        var smallName=$("#smallName").val();       //商品类型
        var sellerName=$("#sellerName").val();    //商家名称
        //$.getJSON("/doGoodsServlet",{"action":"selectPage","index":index,"sellerName":sellerName,"smallName":smallName,"goodsName":goodsName},callback);
        $.getJSON("/doGoodsServlet",{"action":"selectPage","index":index,"goodsName":goodsName},callback);
        function callback(data) {
            for (var i = 0; i <data.length ; i++) {
                $("#tbody1").append("<tr>" +
                    "<td>"+data[i].id+"</td>" +
                    "<td>"+data[i].goodsName+"</td>" +
                    "<td>"+data[i].goodsSmalId+"</td>" +
                    "<td>"+data[i].goodsMoney+"</td>" +
                    "<td>"+data[i].goodsNumber+"</td>" +
                    "<td><img src='GoodsImage/"+data[i].goodsImage+"'></td>" +
                    "<td>"+data[i].goodsCarriage+"</td>" +
                    "<td>"+data[i].goodsType+"</td>" +
                    "<td>"+data[i].goodsSeId+"</td>" +
                    "<td>"+data[i].goodsDiscId+"</td>" +
                    "<td>" +
                    "<a href='javascript:void(0)' onclick='editUpdate("+data[i].id+")'>编辑</a>" +
                    "<a href='javascript:void(0)' onclick='deleteGoods("+data[i].id+",this)'>删除</a>" +
                    "</td>" +
                    "</tr>");
            }
            getPage();
        }
    }

    //模糊查询
    function  Fuzzyquery() {
        $("#tbody1").html("");
        //获取商品名称
        var goodsName=$("#goodsName").val();
        //获取商家名称
        var sellerName=$("#sellerName").val();
        //获取小分类名称
        var smallName=$("#smallName").val();
        $.getJSON("/doGoodsServlet",{"action":"fuzzy","goodsName":goodsName,"sellerName":sellerName,"smallName":smallName},callbackgodes);
        function callbackgodes(data) {
            for (var i = 0; i <data.length ; i++) {
                $("#tbody1").append("<tr>" +
                    "<td>"+data[i].id+"</td>" +
                    "<td>"+data[i].goodsName+"</td>" +
                    "<td>"+data[i].goodsSmalId+"</td>" +
                    "<td>"+data[i].goodsMoney+"</td>" +
                    "<td>"+data[i].goodsNumber+"</td>" +
                    "<td><img src='GoodsImage/"+data[i].goodsImage+"'></td>" +
                    "<td>"+data[i].goodsCarriage+"</td>" +
                    "<td>"+data[i].goodsType+"</td>" +
                    "<td>"+data[i].goodsSeId+"</td>" +
                    "<td>"+data[i].goodsDiscId+"</td>" +
                    "<td>" +
                    "<a href='javascript:void(0)' onclick='editUpdate("+data[i].id+")'>编辑</a>" +
                    "<a href='javascript:void(0)' onclick='deleteGoods("+data[i].id+",this)'>删除</a>" +
                    "</td>" +
                    "</tr>");
            }
            getPage();
        }
    }

    var index=0 ;   //当前页
    var pagede=0;  //总页数
    //获得页数的方法
    function getPage() {
        $.getJSON("/doGoodsServlet","action=getPage",callback,"json");
        function  callback(data) {
            index=parseInt(data.index);
            pagede=parseInt(data.countPages);
            showPage();
        }
    }

    //显示页数的方法
    function  showPage() {
        $("#pageNo").html(index);
        $("#totalPages").html(pagede);
    }
    //下一页
    function nextpage(){
        if(index<pagede){
            index++;
        }else{
            index=pagede;
            alert("已经是最后一页了！");
        }
        CountSelect();
    }
    //上一页
    function Previouspage(){
        if(index>1){
            index--;
        }else{
            index=1;
            alert("已经是第一页了！");
        }
        CountSelect(index);
    }
    //首页
    function homepage(){
        index=1;
        CountSelect();
    }
    //尾页
    function Tailpage(){
        index=pagede;
        CountSelect();
    }

    //添加小分类的下拉列表
    function addSmall() {
        $.getJSON("/SmallServlet", "action=select", callback);
        function callback(data) {
            $(data).each(function () {
                $("[id=smallName]").append("<option value='" + this.smallId + "'>" + this.smallName + "</option>");
            })
        }
    }

    //添加商家名称的下拉列表
    function addSeller() {
        $.getJSON("/SmallServlet", "action=select1", callback);
        function callback(data) {
            $(data).each(function () {
                $("[id=sellerName]").append("<option value='" + this.id + "'>" + this.sellerName + "</option>");
            })
        }
    }

    //删除商品信息
    function deleteGoods(id,btn) {
        $.getJSON("/doGoodsServlet","action=delete&id="+id,callback);
        function callback(data) {
            var conf=confirm("确定删除吗？");
            if(conf!=true){
                alert("取消了删除");
            }
            if(data.flag=="true"){
                $(btn).parent().parent().remove();
                alert("删除成功!");
            }else{
                alert("删除失败!");
            }
        }
    }

    //跳转到修改
    function editUpdate(GoodsID) {
        location.href="Seller/goodsUpdate.jsp?GoodsID="+GoodsID;
    }
</script>
</html>
